package com.bus.boot.manager.assist.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bus.boot.dao.assist.BusGuestbookDao;
import com.bus.boot.entity.assist.BusGuestbook;
import com.bus.boot.entity.assist.BusGuestbookExt;
import com.bus.boot.manager.assist.BusGuestbookCtgMng;
import com.bus.boot.manager.assist.BusGuestbookExtMng;
import com.bus.boot.manager.assist.BusGuestbookMng;
import com.bus.common.hibernate4.Updater;
import com.bus.common.page.Pagination;
import com.bus.core.entity.BusUser;
import com.bus.core.entity.BusUserExt;
import com.bus.core.manager.BusUserExtMng;

@Service
@Transactional
public class BusGuestbookMngImpl implements BusGuestbookMng {
	@Transactional(readOnly = true)
	public Pagination getPage(Integer siteId, Integer ctgId,Integer ctgIds[],
			Integer userId, Boolean recommend,Short checked,
			boolean desc, boolean cacheable, int pageNo,int pageSize) {
		return dao.getPage(siteId, ctgId,ctgIds,userId, recommend, checked, desc, cacheable,
				pageNo, pageSize);
	}

	@Transactional(readOnly = true)
	public List<BusGuestbook> getList(Integer siteId, Integer ctgId,
			Integer userId,Boolean recommend, Short checked, boolean desc,
			boolean cacheable, int first, int max) {
		return dao.getList(siteId, ctgId,userId, recommend, checked, desc, cacheable,
				first, max);
	}

	@Transactional(readOnly = true)
	public BusGuestbook findById(Integer id) {
		BusGuestbook entity = dao.findById(id);
		return entity;
	}

	public BusGuestbook save(BusGuestbook bean, BusGuestbookExt ext,
			Integer ctgId, String ip) {
		bean.setCtg(busGuestbookCtgMng.findById(ctgId));
		bean.setIp(ip);
		bean.setCreateTime(new Timestamp(System.currentTimeMillis()));
		bean.init();
		dao.save(bean);
		busGuestbookExtMng.save(ext, bean);
		return bean;
	}

	public BusGuestbook save(BusUser member, Integer siteId, Integer ctgId,
			String ip, String title, String content, String email,
			String phone, String qq) {
		BusGuestbook guestbook = new BusGuestbook();
		guestbook.setMember(member);
		guestbook.setIp(ip);
		BusGuestbookExt ext = new BusGuestbookExt();
		ext.setTitle(title);
		ext.setContent(content);
		ext.setEmail(email);
		ext.setPhone(phone);
		ext.setQq(qq);
		//累计今日留言数
		if(member!=null){
			BusUserExt userExt=member.getUserExt();
			userExt.setTodayGuestbookTotal(userExt.getTodayGuestbookTotal()+1);
			userExtMng.update(userExt, member);
		}
		return save(guestbook, ext, ctgId, ip);
	}

	public BusGuestbook update(BusGuestbook bean, BusGuestbookExt ext,
			Integer ctgId) {
		Updater<BusGuestbook> updater = new Updater<BusGuestbook>(bean);
		bean = dao.updateByUpdater(updater);
		bean.setCtg(busGuestbookCtgMng.findById(ctgId));
		busGuestbookExtMng.update(ext);
		return bean;
	}

	public BusGuestbook deleteById(Integer id) {
		BusGuestbook bean = dao.deleteById(id);
		return bean;
	}

	public BusGuestbook[] deleteByIds(Integer[] ids) {
		BusGuestbook[] beans = new BusGuestbook[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	
	public BusGuestbook[] checkByIds(Integer[] ids,BusUser checkUser,Short checkStatus) {
		BusGuestbook[] beans = new BusGuestbook[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = checkById(ids[i],checkUser,checkStatus);
		}
		return beans;
	}
	
	private BusGuestbook checkById(Integer id,BusUser checkUser,Short checkStatus){
		BusGuestbook bean=findById(id);
		Updater<BusGuestbook> updater = new Updater<BusGuestbook>(bean);
		bean = dao.updateByUpdater(updater);
		if(checkStatus!=null && !checkStatus.equals(0)){
			bean.setAdmin(checkUser);
		}
		bean.setChecked(checkStatus);
		return bean;
	}
	

	private BusGuestbookCtgMng busGuestbookCtgMng;
	private BusGuestbookExtMng busGuestbookExtMng;
	private BusGuestbookDao dao;
	@Autowired
	private BusUserExtMng userExtMng;

	@Autowired
	public void setDao(BusGuestbookDao dao) {
		this.dao = dao;
	}

	@Autowired
	public void setBusGuestbookExtMng(BusGuestbookExtMng busGuestbookExtMng) {
		this.busGuestbookExtMng = busGuestbookExtMng;
	}

	@Autowired
	public void setBusGuestbookCtgMng(BusGuestbookCtgMng busGuestbookCtgMng) {
		this.busGuestbookCtgMng = busGuestbookCtgMng;
	}

}